//
//  LDSecArtController.m
//  Gift
//
//  Created by 李迪 on 15/11/17.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDSecArtController.h"
#import "LDClassRoomCell.h"
#import "BaseHeader.h"
#import "LDArtifactModel.h"
#import "LDSecPushController.h"

@interface LDSecArtController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *tab;
@property (nonatomic, retain)NSMutableArray *array;

@end

@implementation LDSecArtController

-(void)dealloc{
    
    [self.tab release];
    [super dealloc];
}

//tab对象
- (void)craeteTab{
    
    self.tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 113) style:UITableViewStylePlain];
    [self.tab registerClass:[LDClassRoomCell class] forCellReuseIdentifier:@"ClassRoomCell"];
    self.tab.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tab.dataSource = self;
    self.tab.delegate = self;
    self.tab.rowHeight = 100;
    [self.view addSubview:self.tab];
    
}

//tab协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LDClassRoomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClassRoomCell"];
    
    LDArtifactModel *model = [self.array objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LDSecPushController *sec = [[LDSecPushController alloc]init];
    sec.model = [self.array objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:sec animated:YES];
    
    [sec release];
}

#pragma mark - 数据解析
- (void)handleData{
    [PFAFNetworking postUrlString:@"http://yuntuapi.amap.com/datasearch/local?tableid=550fa56ce4b050797967f047&city=%E5%85%A8%E5%9B%BD&keywords=&filter=city_name%3A%E5%8C%97%E4%BA%AC&sortrule=sort_index%3A0&limit=20&page=1&key=55f0eadf1e00f8defca60c0f4bb1e700&ts=1446814946615&scode=aff82e8c4477e3942b87aa100e5eaedb" body:nil response:RequestJSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        self.array = [[NSMutableArray array]init];
        for (NSDictionary *dic in [responseObject objectForKey:@"datas"]) {
            LDArtifactModel *model = [LDArtifactModel modelSteDic:dic];
            [self.array addObject:model];
        }
        
        [self.tab reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
        
    }];
    
}

- (void)viewWillAppear:(BOOL)animated{
    if (self.array.count == 0) {
        [self handleData];
        [self.tab reloadData];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self craeteTab];
    [self handleData];
    self.title = @"手工";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.], NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:1.00 blue:1.000 alpha:1.000]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
